package modelo.bd.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import modelo.bd.exception.DAOException;
import modelo.dominio.Compania;

public class CompaniaDAO extends AbstractDAO<Compania> {

    @Override
    public void add(Compania entity) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteWhere(String condition) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void update(Compania entity) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected ArrayList<Compania> select() throws DAOException {
        try {
            con.crear();
            ResultSet resultado = con.ejecutarConsulta(getQuery());
            ArrayList<Compania> cias = new ArrayList<Compania>();
            while (resultado.next()) {
                Compania cia = new Compania();
                cia.setClave(resultado.getString("cmpn_clave"));
                cia.setNombre(resultado.getString("cmpn_descripcion"));
                cias.add(cia);
            }
            resultado.close();
            con.cerrar();
            return cias;
        } catch (ClassNotFoundException ex) {
            throw new DAOException("Error al crear la conexion " + ex.getMessage());
        } catch (SQLException ex) {
            throw new DAOException("Error en DAO Companias " + ex.getMessage());
        } catch (Exception ex) {
            throw new DAOException("Error en el archivo de propiedades " + ex.getMessage());
        }
    }

    @Override
    public ArrayList<Compania> selectAll() throws DAOException {
        this.setQuery("SELECT cmpn_clave, '(' ||cmpn_clave ||') '|| substr(cmpn_descripcion, 1, 45) cmpn_descripcion "
                + "FROM scc_companias");
        return select();
    }

    @Override
    public ArrayList<Compania> selectWhere(String condition) throws DAOException {
        this.setQuery("SELECT cmpn_clave, '(' ||cmpn_clave ||') '|| substr(cmpn_descripcion, 1, 45) cmpn_descripcion "
                + "FROM scc_companias "
                + "WHERE " + condition);
        return select();
    }
}
